package com.ht.managermentserver.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * localdatetime 序列化
 *
 * @author czy
 * @package com.ht.managermentserver.utils
 * @date 2020/6/2 16:59
 */
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {


  private String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

  public LocalDateTimeSerializer(String DEFAULT_DATE_FORMAT) {
    this.DEFAULT_DATE_FORMAT = DEFAULT_DATE_FORMAT;
  }

  public LocalDateTimeSerializer() {
  }

  @Override
  public void serialize(
    final LocalDateTime value, final JsonGenerator gen, final SerializerProvider provider)
    throws IOException {
    gen.writeString(value.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
  }
}
